﻿
#region Usings

using FasterWPF;
using FasterWPFDemo.UserControls;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reactive.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Shapes;
using wpfToolkit = Xceed.Wpf.Toolkit;

#endregion

namespace FasterWPFDemo
{
    public partial class TabItem01 : UserControl, IUserControlGuid
    {       
        #region Events

        void validateButton_ValidateFields(object o, MouseButtonEventArgs ev)
        {
            //Validate Fields Example
            if (thisComposite.GetChildFromComposite<TextBox, Grid>(2, 1).Text == null || (thisComposite.GetChildFromComposite<TextBox, Grid>(2, 1).Text == String.Empty))
            {
                AdornerDecoratorExt.DisplayErrorRectangle(thisComposite.GetChildFromComposite<TextBox, Grid>(2, 1));
            }
            else
            {
                AdornerDecoratorExt.RemoveErrorRectangle(thisComposite.GetChildFromComposite<TextBox, Grid>(2, 1));
            }

            if (thisComposite.GetChildFromComposite<TextBox, Grid>(1, 1).Text == null || (thisComposite.GetChildFromComposite<TextBox, Grid>(1, 1).Text == String.Empty))
            {
                AdornerDecoratorExt.DisplayErrorRectangle(thisComposite.GetChildFromComposite<TextBox, Grid>(1, 1));
            }
            else
            {
                AdornerDecoratorExt.RemoveErrorRectangle(thisComposite.GetChildFromComposite<TextBox, Grid>(1, 1));
            }

            if (thisComposite.GetChildFromComposite<TextBox, Grid>(4, 1).Text == null || (thisComposite.GetChildFromComposite<TextBox, Grid>(4, 1).Text == String.Empty))
            {
                AdornerDecoratorExt.DisplayErrorRectangle(thisComposite.GetChildFromComposite<TextBox, Grid>(4, 1));
            }
            else
            {
                AdornerDecoratorExt.RemoveErrorRectangle(thisComposite.GetChildFromComposite<TextBox, Grid>(4, 1));
            }

            if (thisComposite.GetChildFromComposite<TextBox, Grid>(5, 1).Text == null || (thisComposite.GetChildFromComposite<TextBox, Grid>(5, 1).Text == String.Empty))
            {
                AdornerDecoratorExt.DisplayErrorRectangle(thisComposite.GetChildFromComposite<TextBox, Grid>(5, 1));
            }
            else
            {
                AdornerDecoratorExt.RemoveErrorRectangle(thisComposite.GetChildFromComposite<TextBox, Grid>(5, 1));
            }

            bool anyInvalidFields = false;
            foreach (UIElement uie in thisComposite.GetChildFromComposite<TextBox, Grid>(5, 1).Parent.Cast<Grid>().Children)
            {
                if (ad.AdornerLayer.GetAdorners(uie) != null && ad.AdornerLayer.GetAdorners(uie).Length > 0)
                {
                    anyInvalidFields = true;
                }
            }

            if (anyInvalidFields)
            {
                MessageBox.Show("Please fill in required fields!");
            }
        }

        //SelectionChanged
        void myListBox_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
        {
            if (e.StillHasItemSelected() && !isLoading)
            {
                MessageBox.Show(e.SelectedKey());
            }
            //example to set visited color
            //foreach(Border i in myListBox.Items)
            //{
            //    if (i.Background == Brushes.Turquoise)
            //    {
            //        i.Background = Brushes.Blue;
            //    }
            //}
            //e.SelectedBorder().Background = Brushes.Turquoise;    
        }

        //SelectionChanged
        void myListView_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
        {
            if (e.StillHasItemSelected() && !isLoading)
            {
                MessageBox.Show(e.SelectedKey());
            }
            //example to set visited color
            //foreach(Border i in myListBox.Items)
            //{
            //    if (i.Background == Brushes.Turquoise)
            //    {
            //        i.Background = Brushes.Blue;
            //    }
            //}
            //e.SelectedBorder().Background = Brushes.Turquoise;    
        }

        private void b_Click(object sender, RoutedEventArgs e)
        {

            ContentControl c = myListBox.SelectedContentControl();
            if (c != null)
            {
                c.GetTextRuns<Grid, ListBox>().ForEach(str => { MessageBox.Show(str.ToString()); });
            }
            else
            {
                MessageBox.Show("Please select an item!");
            }

            //myListBox.RemoveByKey(oneGUID);  
            // myListBox.UpdateTextByKey(twoGUID, 0, 1, " Rectangle:");
            string testguid = (myListBox.GetCompositeLookupDictionary().First().Value as Border).GetKey();
            TextBlock txb = myListBox.UpdateTextByKey(testguid, 1, 0, " Sunset:");
            myListBox.UpdateImageByKey(testguid, 0, 0, @"pack://siteoforigin:,,,/Images/sunset.bmp", UriKind.Absolute);

           // myListBox.SubscribeMouseDoubleClickOnBorder(txb);  //still honors predicate!

            //myListBox.ScrollIntoViewByKey(twoGUID);

            //myListBox.SetFontStyleKey("myKey", "Arial", 17D, FontWeights.Bold, FontStyles.Normal);
            //myListBox.SetFontAtKeyByStyleKey("myKey", twoGUID, 0, 1);

            myListBox.SetSelectedKey(testguid);


            // myListBox.SortByText(1, 0, ListBoxExt.SortOrder.Asc);
            // myListBox.SortByText1AndThenBy2(1, 0, 1, 1, ListBoxExt.SortOrder.Asc);
            //tList.ForEach(tup =>
            //{
            //    string x = tup.Item1.ToString();

            //});

          //  myListBox.FilterByText1And2(1, 0, 1, 1, tuple => { return ((tuple.Item1.Contains("Woods") || tuple.Item1.Contains("Desert") || tuple.Item1.Contains("Sunset")) && (tuple.Item2.Contains('1'))); });
            ListBoxSort lsb = new ListBoxSort(CommonExt.SortDataType.String, CommonExt.SortDataType.Integer);
            myListBox.SortByText1AndThenBy2(lsb, 1, 0, 1, 1, CommonExt.SortOrder.Desc, CommonExt.SortOrder.Asc, CommonExt.SortControlType.TextBlock);

            //myListBox.SortByText<ListBox>(1, 1, CommonExt.SortOrder.Asc);

            //myListBox.UpdateTextBlocks<string>(1, 0, txb1 => { 
            //    if (txb1.GetText().Contains("yo!")) { txb1.SetText("hollaback!"); } 
            //    if (txb1.GetText().Contains("Lake")) { txb1.SetText("yo!"); } });

            //myListBox.UpdateTextBlocks<string>(1, 0, 3, 3, txb1 =>
            //{
            //    txb1.Visibility = Visibility.Hidden;

            //});

            //myListBox.UpdateImages<string>(0, 0, 3, 3, img1 =>
            //{
            //    img1.Visibility = Visibility.Hidden;

            //});
            //myListBox.RemoveByKey(twoGUID);
            //myListBox.RemoveAll();
            //myListBox.Dispose();

            //myListBox.AddRowElements(twoGUID, rowGrid =>
            //{
            //    rowGrid.AddText(twoGUID, 0, 1, " test ").AddTextWithToolTipEnabled(twoGUID, 0, 2, twoGUID).SetMouseOverColor(0, 0, Brushes.BlanchedAlmond);

            //});

            //myListBox.SetSelectedKey(twoGUID);
        }

        void cb_SelectionChanged(object sender, SelectionChangedEventArgs scea)
        {
            // MessageBox.Show("ComboBox Selected Changed: " + (((ComboBox)scea.Source).SelectedItem).Cast<Border>().GetText<ComboBox>(0, 0).ToString());

        }

        void my_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            // e.GetLines(sender).ForEach(line => { System.Windows.MessageBox.Show(line); });  //1. works regardless what type of element was clicked, returning lines for all the elements in that row that are text

            // System.Windows.MessageBox.Show(e.GetTextRun());  //2. works only if a text run was clicked else returns empty string

            System.Windows.MessageBox.Show(e.GetKey(sender));
        }

        void cb1_SelectionChanged(object sender, SelectionChangedEventArgs scea)
        {
            if (scea != null && scea.AddedItems != null && scea.AddedItems.Count > 0)
            {
                MessageBox.Show("ComboBox Selected Changed: " + scea.AddedItems[0].Cast<Border>().GetText<ComboBox>(0, 0).ToString());
                //((ComboBox)sender).RemoveByKey<ComboBox>(scea.AddedItems[0].Cast<Border>().GetKey());
            }
        }

        #endregion
    }
}
